package com.atguigu.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.util.List;

/**
 * Date:2021/3/8
 * Author:ybc
 * Description:
 */
public class XPathTest {

    @Test
    public void testXPath() throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read("user.xml");
        //若Xpath表达式匹配了一个标签，使用selectSingleNode()，若匹配多个使用selectNodes()
        /*Node node = document.selectSingleNode("/user/userInfo/username");
        System.out.println(node.getText());*/
        List list = document.selectNodes("/user/userInfo/username");
        for (Object o : list) {
            Node node = (Node) o;
            System.out.println(node.getText());
        }
    }

}
